// pages/juzao/juzao.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        baseURL: wx.baseURL,
        indicatorDots: true,
        vertical: false,
        autoplay: true,
        interval: 2000,
        duration: 500,
        adList: [],
        banners: [],
        categorys: [],
        recommends:[],
        pageNum:1,
        total:0,
        loading:"数据加载中..."
    },
    getBanners() {
        return new Promise((resolve,reject) => {
            wx.get('/index/banners').then((res) => {
                this.setData({
                    banners: res
            })
            resolve()
        })
        })
     },
        getNews() {
            return new Promise((resolve,reject) => {
                 wx.get('/index/news').then((res) => {
                    this.setData({
                        adList: res
                })
                resolve()
            })
        })

    },
        getCategorys() {
            return new Promise((resolve,reject) =>{
                wx.get('/index/categorys').then((res) => {
                    this.setData({
                        categorys: res
                })
                resolve()
            })
            })
        },
        getRecommends() {
            return new Promise((resolve,reject) =>{
                this.setData({loading:'数据加载中...'})
                wx.get('/index/recommends',{
                    pageNum : this.data.pageNum
                }).then((res) => {
                    this.setData({
                        loading:'小主，已经到底了哦~',
                        recommends: this.data.recommends.concat( res.rows),
                        total:res.total
                })
                resolve()
            })
            })
        },

        tapNameBig(even) {
            console.log("tapNameBig", even);
        },
        tapName(event) {
            console.log("点击成功", event);
            let url = event.currentTarget.dataset.path
            wx.showModal({
                title: "提示",
                cancelColor: 'cancelColor',
                content: '是否跳转到' + url + '页面',
                showCancel: false,
                success(e) {
                    console.log(url);
                    wx.switchTab({
                        url
                    })
                }
            })
        },

        onList(){
            Promise.all([this.getBanners(),this.getNews(),this.getCategorys(),this.getRecommends()]).then(() =>{
                console.log('所有数据加载完成')
            })
        },
        /**
         * 生命周期函数--监听页面加载
         */
        onLoad: function (options) {
            this.onList()
        },

        /**
         * 生命周期函数--监听页面初次渲染完成
         */
        onReady: function () {

        },

        /**
         * 生命周期函数--监听页面显示
         */
        onShow: function () {

        },

        /**
         * 生命周期函数--监听页面隐藏
         */
        onHide: function () {

        },

        /**
         * 生命周期函数--监听页面卸载
         */
        onUnload: function () {

        },

        /**
         * 页面相关事件处理函数--监听用户下拉动作
         */
        onPullDownRefresh: function () {
            this.data.pageNum = 1
            this.data.recommends = []
            this.onList()
            // this.getRecommends()
            // Promise.all([this.getBanners(),this.getNews(),this.getCategorys(),this.getRecommends()]).then(() =>{
            //     wx.stopPullDownRefresh()
            // }).catch(e =>{
            //     wx.stopPullDownRefresh()
            // })
        },

        /**
         * 页面上拉触底事件的处理函数
         */
        onReachBottom: function () {
            if(this.data.total >this.data.recommends.length){
                this.data.pageNum ++
                this.getRecommends() 

            }
        },

        /**
         * 用户点击右上角分享
         */
        onShareAppMessage: function () {

        }
    })